﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace menu
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Windows.Forms;
    using static System.Windows.Forms.VisualStyles.VisualStyleElement;

    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            
        }

        private void InitializeTreeView()
        {
            // Get user's directory
            string userDirectory = @"../";

            // Create root node
            TreeNode rootNode = new TreeNode(userDirectory);
            rootNode.Tag = userDirectory;
            treeView2.Nodes.Add(rootNode);

            // Populate tree with subdirectories
            PopulateTreeView(rootNode, userDirectory);
        }

        private void PopulateTreeView(TreeNode parentNode, string directoryPath)
        {
            try
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
                foreach (var subDirectory in directoryInfo.GetDirectories())
                {
                    TreeNode subNode = new TreeNode(subDirectory.Name);
                    subNode.Tag = subDirectory.FullName;
                    parentNode.Nodes.Add(subNode);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error populating tree view: {ex.Message}");
            }
        }

        private void treeView2_AfterSelect(object sender, TreeViewEventArgs e)
        {
            // Clear list view
            listView2.Items.Clear();

            // Get selected directory
            string selectedDirectory = e.Node.Tag.ToString();

            try
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(selectedDirectory);
                foreach (var file in directoryInfo.GetFiles())
                {
                    if (file.Extension == ".txt")
                    {
                        // Add .txt files to list view
                        ListViewItem item = new ListViewItem(file.Name);
                        item.SubItems.Add(file.FullName);
                        listView2.Items.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error reading files: {ex.Message}");
            }
        }

        private void listView2_DoubleClick(object sender, EventArgs e)
        {
            // Open selected .txt file
            if (listView2.SelectedItems.Count > 0)
            {
                string filePath = listView2.SelectedItems[0].SubItems[1].Text;
                Process.Start(filePath);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            InitializeTreeView();
        }

        private void 创建新文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var dialog = new SaveFileDialog())
            {
                dialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    // Create a new empty text file
                    File.Create(dialog.FileName);
                }
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (listView2.SelectedItems.Count > 0)
            {
                string filePath = listView2.SelectedItems[0].SubItems[1].Text;
                Clipboard.SetText(filePath);
                MessageBox.Show($"File path copied to clipboard:\n{filePath}");
            }
            else
            {
                MessageBox.Show("Please select a file to copy.");
            }
        }
    }
  
}
